//
//  LBEmptyDataSystemView.m
//  LBEmptyDataSystemView
//
//  Created by 杨庆人 on 2017/9/28.
//  Copyright © 2017年 杨庆人. All rights reserved.
//

#import "LBEmptyDataSystemView.h"
#import <Masonry/Masonry.h>
#import <ReactiveObjC/ReactiveObjC.h>
#import <YLGIFImage/YLImageView.h>
#import <YLGIFImage/YLGIFImage.h>

@interface LBEmptyDataSystemView ()
@property (nonatomic,strong)YLImageView *imageView;
@end

@implementation LBEmptyDataSystemView

- (instancetype)initWithLBEmptyDataType:(LBEmptyDataSystemType)type title:(NSString *)title detail:(NSString *)detail block:(LBEmptyDataBlock)block customBtnName:(NSString *)customBtnName {
    
    if (self = [super init]) {
        
        switch (type) {
         
            case LBEmptyDataTypeNeedLogin:
            {
                [self addSubview:self.imageView];
                [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.bottom.mas_equalTo(self.mas_centerY);
                    make.centerX.mas_equalTo(self.mas_centerX);
                    make.size.mas_equalTo(CGSizeMake(200, 200));
                }];
                
                [self addSubview:self.titleLabel];
                [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.mas_equalTo(self.imageView.mas_bottom).offset(20);
                    make.left.offset(20);
                    make.right.offset(-20);
                }];
                
                UIButton *loginBtn = [self emptyDataLoginButton];
                [self addSubview:loginBtn];
                [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.offset(20);
                    make.right.offset(-20);
                    make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(35);
                    make.height.mas_equalTo(47);
                }];
                self.imageView.image = [UIImage imageNamed:@"LBEmptyData.bundle/noLogin"];
                self.titleLabel.text = @"请登录后进行操作";
                
                [[loginBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
                    !block ? :block();
                }];
            }
                break;
            case LBEmptyDataTypeNoData:
            {
                [self addSubview:self.imageView];
                [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.bottom.mas_equalTo(self.mas_centerY);
                    make.centerX.mas_equalTo(self.mas_centerX);
                    make.size.mas_equalTo(CGSizeMake(200, 200));
                }];
                
                [self addSubview:self.titleLabel];
                [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.mas_equalTo(self.imageView.mas_bottom).offset(20);
                    make.left.offset(20);
                    make.right.offset(-20);
                }];
                self.imageView.image = [YLGIFImage imageNamed:@"LBEmptyData.bundle/lbNoDate_gif.gif"];
                self.titleLabel.text = title;
                if (detail) {
                    [self addSubview:self.detailLabel];
                    [self.detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(15);
                        make.left.offset(20);
                        make.right.offset(-20);
                    }];
                    
                    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
                    
                    [paragraphStyle setLineSpacing:8];
                    
                    NSMutableAttributedString  *attrAtr = [[NSMutableAttributedString alloc] initWithString:detail];
                    [attrAtr  addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [detail length])];

                    [self.detailLabel setAttributedText:attrAtr];
                    
                    self.detailLabel.textAlignment = NSTextAlignmentCenter;
                }
                
                if (block) {
                    UIButton *noDataBtn = [self businessButton];
                    [self addSubview:noDataBtn];
                    [noDataBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                        make.right.offset(-95);
                        make.left.offset(95);
                        make.top.mas_equalTo(detail ? self.detailLabel.mas_bottom : self.titleLabel.mas_bottom).offset(35);
                        make.height.mas_equalTo(44);
                    }];
                    if (customBtnName) {
                        [noDataBtn setTitle:customBtnName forState:UIControlStateNormal];
                    }
                    else {
                        [noDataBtn setTitle:@"重新加载" forState:UIControlStateNormal];
                    }
                    [[noDataBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {                    !block ? :block();
                    }];
                }
            }
                break;
            case LBEmptyDataTypeNoDataNoImage: {

                [self addSubview:self.titleLabel];
                [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerY.mas_equalTo(self.mas_centerY);
                    make.left.offset(20);
                    make.right.offset(-20);
                }];
                self.titleLabel.text = title;

                if (detail) {
                    [self addSubview:self.detailLabel];
                    [self.detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(15);
                        make.left.offset(20);
                        make.right.offset(-20);
                    }];
                    
                    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
                    
                    [paragraphStyle setLineSpacing:8];
                    
                    NSMutableAttributedString  *attrAtr = [[NSMutableAttributedString alloc] initWithString:detail];
                    [attrAtr  addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [detail length])];
                    
                    [self.detailLabel setAttributedText:attrAtr];
                    
                    self.detailLabel.textAlignment = NSTextAlignmentCenter;
                }
            }
                break;
            default:
                break;
        }
    }
    return self;
}

- (YLImageView *)imageView {
    if (!_imageView) {
        _imageView = [[YLImageView alloc] init];
        _imageView.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _imageView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = [UIFont systemFontOfSize:20];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.textColor = [UIColor colorWithRed:69/255.0 green:69/255.0 blue:69/255.0 alpha:1];
        _titleLabel.numberOfLines = 0;
    }
    return _titleLabel;
}

- (UILabel *)detailLabel {
    if (!_detailLabel) {
        _detailLabel = [[UILabel alloc] init];
        _detailLabel.font = [UIFont systemFontOfSize:15];
        _detailLabel.textAlignment = NSTextAlignmentCenter;
        _detailLabel.textColor = [UIColor colorWithRed:161/255.0 green:164/255.0 blue:169/255.0 alpha:1];
        _detailLabel.numberOfLines = 0;
    }
    return _detailLabel;
}

- (UIButton *)emptyDataLoginButton {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundColor:[UIColor colorWithRed:73/255.0 green:144/255.0 blue:226/255.0 alpha:1]];
    button.layer.cornerRadius = 5;
    [button setTitle:@"立即登录" forState:UIControlStateNormal];

    return button;
}


- (UIButton *)emptyDataReloadButton {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.layer.borderWidth = 1;
    button.layer.borderColor = [UIColor colorWithRed:161/255.0 green:164/255.0 blue:169/255.0 alpha:1].CGColor;
    [button setTitleColor:[UIColor colorWithRed:161/255.0 green:164/255.0 blue:169/255.0 alpha:1] forState:UIControlStateNormal];
    button.layer.cornerRadius = 2;
    button.titleLabel.font = [UIFont systemFontOfSize:14];
    
    return button;
}

- (UIButton *)businessButton {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:[UIImage imageNamed:@"LBEmptyData.bundle/EmptyResGradientBtn"] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:18];
    button.layer.cornerRadius = 21;
    button.clipsToBounds = YES;
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    return button;
}

@end
